<template>
  <div ref="chartContainer" class="chart-container"></div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'ScenicYear',
  data() {
    return {
      chart: null,
      yearData: [
        { year: '2007', count: 33 },
        { year: '2008', count: 6 },
        { year: '2009', count: 8 },
        { year: '2010', count: 12 },
        { year: '2011', count: 15 },
        { year: '2012', count: 19 },
        { year: '2013', count: 21 },
        { year: '2014', count: 25 },
        { year: '2015', count: 28 },
        { year: '2016', count: 30 },
        { year: '2017', count: 32 },
        { year: '2018', count: 35 },
        { year: '2019', count: 38 },
        { year: '2020', count: 40 },
        { year: '2021', count: 42 },
        { year: '2022', count: 45 },
        { year: '2023', count: 48 }
      ]
    }
  },
  mounted() {
    this.initChart()
    window.addEventListener('resize', this.resizeChart)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeChart)
    this.chart && this.chart.dispose()
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.chartContainer, null, {
        renderer: 'canvas'
      })
      this.updateChart()
    },
    updateChart() {
      const option = {
        backgroundColor: 'transparent',
        title: {
          text: '5A级景区评定年份分布',
          subtext: '数据来源：文化和旅游部',
          left: 'center',
          textStyle: {
            color: '#8B4513',
            fontSize: 16,
            fontWeight: 'normal'
          },
          subtextStyle: {
            color: '#8B4513',
            fontSize: 12
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          backgroundColor: 'rgba(255, 255, 255, 0.9)',
          borderColor: '#8B4513',
          textStyle: {
            color: '#8B4513'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: this.yearData.map(item => item.year),
          axisLabel: {
            color: '#8B4513',
            rotate: 45
          },
          axisLine: {
            lineStyle: {
              color: '#8B4513'
            }
          }
        },
        yAxis: {
          type: 'value',
          name: '景区数量',
          nameTextStyle: {
            color: '#8B4513'
          },
          axisLabel: {
            color: '#8B4513'
          },
          axisLine: {
            lineStyle: {
              color: '#8B4513'
            }
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(139, 69, 19, 0.1)'
            }
          }
        },
        series: [
          {
            name: '累计景区数量',
            type: 'bar',
            data: this.yearData.map(item => item.count),
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: '#8B4513' },
                { offset: 1, color: '#DEB887' }
              ])
            }
          }
        ]
      }
      this.chart.setOption(option)
    },
    resizeChart() {
      this.chart && this.chart.resize()
    }
  }
}
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
}
</style> 